package at.ac.univie.bi.transformation.service.input;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import at.ac.univie.bi.transformation.model.input.Patient;
import at.ac.univie.bi.transformation.persistence.DBHandler;

public class PatientService extends AbstractInputEntityService{

	List<Patient> patients = new ArrayList<Patient>();
	
	public void readAll(){
		patients = selectPatients();
	}
	
	public static List<Patient> selectPatients()
	  {
		  String tableName = "Patient";
		  ArrayList<Patient> patients = new ArrayList<Patient>();
		  Statement stmt = null;
		  Connection oldDB = DBHandler.getOldDB();
		  
	      try
	      {
	          stmt = oldDB.createStatement();
	          ResultSet results = stmt.executeQuery("select * from " + tableName);
	          

	          while(results.next())
	          {
	        	  int id = results.getInt(1);
	              String firstname = results.getString(2);
	              String lastname = results.getString(3);
	              int birthdate = results.getInt(4);
	              
	              Patient newPatient = new Patient();
	              newPatient.setOldid(id);
	              newPatient.setFirstname(firstname);
	              newPatient.setLastname(lastname);
	              newPatient.setBirthdate(birthdate);
	              
	              patients.add(newPatient);
	          }
	          results.close();
	          stmt.close();

	      }
	      catch (SQLException sqlExcept)
	      {
	          sqlExcept.printStackTrace();
	      }
	      
	      
	      return patients;
	  }

	public List<Patient> getPatients() {
		return patients;
	}

	public void setPatients(List<Patient> patients) {
		this.patients = patients;
	}

}
